<template>
<highlightjs language="JavaScript" highlightAuto top :code="code" />
</template>
<script>
export default {
  setup () {
    const code = `
    import 'ol/ol.css'
    import { Map, View } from 'ol'
    import Tile from 'ol/layer/Tile'
    import OSM from 'ol/source/OSM'

    const state = reactive({
      map: null,
      mapBox: ref(null)
    })
    const init = () => {
      state.map = new Map({
        target: state.mapBox,
        layers: [
          new Tile({
            source: new OSM()
          })
        ],
        view: new View({
          // 地图视图
          projection: 'EPSG:4326', // 坐标系，有EPSG:4326和EPSG:3857
          center: [116.41400, 39.91500], // 北京坐标
          rotation: 0,
          zoom: 12 // 地图缩放级别（打开页面时默认级别）
        })
      })
    }

    const rotateLeft = () => {
      const currentRotation = state.map.getView().getRotation()
      state.map.getView().setRotation(currentRotation + 1)
    }

    const rotateRight = () => {
      const currentRotation = state.map.getView().getRotation()
      state.map.getView().setRotation(currentRotation - 1)
    }
    `
    return {
      code
    }
  }
}
</script>
